/*
 * CartFileOps.java
 * 
 * Created by demory on Sep 28, 2009, 4:31:08 PM
 * 
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.fpdev.apps.cart.modules;

import org.fpdev.apps.cart.*;
import java.io.File;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.fpdev.apps.cart.network.Bundler;
import org.fpdev.apps.cart.network.Line;
import org.fpdev.core.SysProps;

/**
 *
 * @author demory
 */
public class FileOps extends CartModule {

  private List<String> recentFiles_;

  public FileOps(Cartographer cart) {
    super(cart);

    recentFiles_ = new LinkedList<String>();
    String rFilesStr = cart_.getProperties().getProperty(SysProps.RECENT_FILES);
    if(rFilesStr != null && rFilesStr.length() > 0) {
      for(String filename : rFilesStr.split(";"))
        recentFiles_.add(filename);
    }
  }

  public ID getID() {
    return ID.FILE;
  }

  public CartEventResult handleEvent(CartEvent event) {

    CartEventResult result = new CartEventResult();

    switch(event.getType()) {
      case FILE_NEW:
        newFile();
        break;
      case FILE_LOAD:
        if(event.propertyDefined("filename"))
          loadFile(new File(event.getStringProperty("filename")));
        else loadFile();
        break;
      case FILE_SAVE:
        if(event.getEditor() == null) {//!event.propertyDefined("eid")) {
          cart_.getGUI().msg("Cannot save; no active editor");
          break;
        }
        saveFile(event.getEditor());
        break;
      case FILE_SAVE_AS:
        if(event.getEditor() == null) { //if(!event.propertyDefined("eid")) {
          cart_.getGUI().msg("Cannot save; no active editor");
          break;
        }
        saveFileAs(event.getEditor());
        break;
      case FILE_CLEAR_RECENT:
        clearRecentFiles();
        break;
    }

    return result;
  }

  private void newFile() {
    Editor ed = cart_.getEditorFactory().createBlankEditor();
    cart_.addEditor(ed);
  }

  private void loadFile() {
    JFileChooser chooser = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter("5P-Cartographer Files", "fpc");
    chooser.setFileFilter(filter);
    int returnVal = chooser.showOpenDialog(cart_.getGUI());
    if (returnVal == JFileChooser.APPROVE_OPTION) {
      loadFile(chooser.getSelectedFile());
    }
  }

  private void loadFile(File file) {
    if(!file.exists()) {
      cart_.getGUI().msg("File does not exist: "+file.getPath());
      return;
    }
    addRecentFile(file.getPath());
    //cart_.clearCurrent();
    Editor editor = cart_.getEditorFactory().createEditorFromFile(file);
    /*for(Line line : editor.getNetwork().getLines())
      cart_.getGUI().getControlPanel().getLineSelector().addLine(line);*/
    new Bundler(editor.getDocument().getNetwork());

    cart_.addEditor(editor);
    //editor.getFrame().getCanvas().repaint();
  }

  private void saveFile(Editor ed) {
    if(!ed.getDocument().hasActiveFile()) {
      cart_.getGUI().msg("No active file!");
      return;
    }
    ed.getDocument().writeXMLFile();
  }

  private void saveFileAs(Editor ed) {
    JFileChooser chooser = new JFileChooser();
    FileNameExtensionFilter filter = new FileNameExtensionFilter("5P-Cartographer Files", "fpc");
    chooser.setFileFilter(filter);
    int returnVal = chooser.showOpenDialog(cart_.getGUI());
    if (returnVal == JFileChooser.APPROVE_OPTION)
      ed.getDocument().writeXMLFile(chooser.getSelectedFile());
  }

  public List<String> getRecentFiles() {
    return recentFiles_;
  }

  public void addRecentFile(String filename) {
    if(recentFiles_.contains(filename)) recentFiles_.remove(filename);
    recentFiles_.add(0, filename);
    while(recentFiles_.size() > 10) {
      recentFiles_.remove(recentFiles_.size()-1);
    }
    String rFilesStr = "";
    for(String fn : recentFiles_) {
      rFilesStr += (fn + ";");
    }
    rFilesStr = rFilesStr.substring(0, rFilesStr.length()-1);
    cart_.getProperties().setProperty(SysProps.RECENT_FILES, rFilesStr);
    cart_.writeProperties();
    cart_.getGUI().getCartMenuBar().updateRecentFilesMenu(recentFiles_);
  }

  private void clearRecentFiles() {
    recentFiles_.clear();
    cart_.getProperties().setProperty(SysProps.RECENT_FILES, "");
    cart_.writeProperties();
    cart_.getGUI().getCartMenuBar().updateRecentFilesMenu(recentFiles_);
  }


}
